import tornado
import tornado.ioloop
import tornado.web
import tornado.gen
from datetime import datetime
from test.monitor import Monitor

global g_monitor
port = 4030


class ClassifierHandler(tornado.web.RequestHandler):
    def post(self):
        # TODO Something you need
        # work....
        # 统计Summary，包括请求次数和每次耗时
        g_monitor.set_prometheus_request_summary(self)
        self.write("OK")


class PingHandler(tornado.web.RequestHandler):
    def head(self):
        print('INFO', datetime.now(), "/ping Head.")
        g_monitor.set_prometheus_request_summary(self)
        self.write("OK")

    def get(self):
        print('INFO', datetime.now(), "/ping Get.")
        g_monitor.set_prometheus_request_summary(self)
        self.write("OK")


class MetricsHandler(tornado.web.RequestHandler):
    def get(self):
        print('INFO', datetime.now(), "/metrics Get.")
        g_monitor.set_prometheus_request_summary(self)

        # 通过Metrics接口返回统计结果
        g_monitor.get_prometheus_metrics_info(self)


def make_app():
    return tornado.web.Application([
        (r"/ping?", PingHandler),
        (r"/metrics?", MetricsHandler),
        (r"/work?", ClassifierHandler)
    ])


if __name__ == "__main__":
    g_monitor = Monitor()

    app = make_app()
    app.listen(port)
    tornado.ioloop.IOLoop.current().start()
